
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import javax.swing.*;
import java.util.*;
	
class Flood extends JFrame
{
	java.util.List<Square> squaresList = new ArrayList<Square>();
	
	int movesTotal = 0;
	int movesRemaining = 0;
	int colorChoice = 1;
	int squareNumber = 0;
	int squareSize = 0;

	
	Color colours[] = {Color.red, Color.blue, Color.green, Color.orange, Color.cyan, Color.yellow};
	String colourImages[] = {"red.png", "blue.png", "green.png", "orange.png", "cyan.png", "yellow.png"};
	Square[][] squares; 
	
	JTextArea scoreDisplay = new JTextArea("Moves Remaining: " + movesRemaining);
	JTextField squaresField = new JTextField("Enter number of squares: ");
	
	JButton submitOptions = new JButton("Done!");
	JButton optionsButton = new JButton("Options.");
	JButton buttonReset = new JButton("Reset");
	
	ImageIcon icon0 = new ImageIcon(colourImages[0]);
	ImageIcon icon1 = new ImageIcon(colourImages[1]);
	ImageIcon icon2 = new ImageIcon(colourImages[2]);
	ImageIcon icon3 = new ImageIcon(colourImages[3]);
	ImageIcon icon4 = new ImageIcon(colourImages[4]);
	ImageIcon icon5 = new ImageIcon(colourImages[5]);
	
	JButton button5 = new JButton(icon5);
	JButton button4 = new JButton(icon4);
	JButton button3 = new JButton(icon3);
	JButton button2 = new JButton(icon2);
	JButton button1 = new JButton(icon1);
	JButton button0 = new JButton(icon0);
				
	JPanel panel = new JPanel();
	
	Image rofl = imageFromFile("intro.png");
	JLabel lol = new JLabel( "" + rofl );

	public static void main(String[] args) {
		new Flood();
	}
	
	public Flood()
	{
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout() );
		add(panel, BorderLayout.SOUTH);
	
		setVisible(true);
		options();
		
		
	}
	public void options()
	{
		
		panel.removeAll();
		panel.add(lol);
		setBounds(0,0,300,300);
		panel.add(squaresField);
		panel.add(submitOptions);
		squaresField.setPreferredSize(new Dimension(150,50));
		submitOptions.addActionListener(new clickerThingy());
		submitOptions.setPreferredSize(new Dimension(100,50));
	}
	public void reset()
	{
	movesRemaining = movesTotal;
	setup();
	}
	private class clickerThingy implements ActionListener 
	{
	
		
		public void actionPerformed (ActionEvent e) {
			JButton clicked = (JButton) e.getSource();
			if (clicked == button0)  initialCheck( colours[0] ); 
			if (clicked == button1)  initialCheck( colours[1] );
			if (clicked == button2)  initialCheck( colours[2] );
			if (clicked == button3)  initialCheck( colours[3] );
			if (clicked == button4)  initialCheck( colours[4] );
			if (clicked == button5)  initialCheck( colours[5] );
			if (clicked == optionsButton) options();
			if (clicked == buttonReset) reset();
			if (clicked == submitOptions) 
			{
				try
				{
				squareNumber = Integer.parseInt(squaresField.getText()) - 1;
				sizer(squareNumber); 
				}
				catch (Exception q)
				{
				}
			}
			repaint();
			scoreDisplay.setText("Moves Remaining: " + movesRemaining);
		}
	}
	public void sizer(int squareNumber)
	{
		squareSize = 700 / squareNumber;
		movesTotal = (squareNumber * 2) - 2 ;
		movesRemaining = movesTotal;
		setup();
	}
	
	
	
	
	
	public void setup()
	{	
		panel.removeAll();
		setBounds(0,0,700,775);
		
		panel.add(optionsButton);
		optionsButton.addActionListener(new clickerThingy() );
		optionsButton.setPreferredSize(new Dimension(100,50));
		panel.add(scoreDisplay);
		panel.add(buttonReset);
		buttonReset.addActionListener(new clickerThingy() );
		buttonReset.setPreferredSize(new Dimension(100,50));
		panel.add(button0);	
		button0.addActionListener(new clickerThingy());
		panel.add(button1);
		button1.addActionListener(new clickerThingy());
		panel.add(button2);
		button2.addActionListener(new clickerThingy());
		panel.add(button3);
		button3.addActionListener(new clickerThingy());
		panel.add(button4);
		button4.addActionListener(new clickerThingy());
		panel.add(button5);
		button5.addActionListener(new clickerThingy());
		
		
		button5.setPreferredSize( new Dimension(50,50));
		button4.setPreferredSize( new Dimension(50,50));
		button3.setPreferredSize( new Dimension(50,50));
		button2.setPreferredSize( new Dimension(50,50));
		button1.setPreferredSize( new Dimension(50,50));
		button0.setPreferredSize( new Dimension(50,50));
		
		switch(colorChoice) 
		{
			case 1: { Color colours[] = {Color.red, Color.blue, Color.green, Color.orange, Color.cyan, Color.yellow}; break;}
			case 2: {Color colours[] = {Color.red, Color.blue, Color.green, Color.orange, Color.cyan, Color.yellow}; break;}
			default: {Color colours[] = {Color.red, Color.blue, Color.green, Color.orange, Color.cyan, Color.yellow}; break;}
		}
			
		squares = new Square[squareNumber][squareNumber];
		
		for ( int i = 0; i < squareNumber ; i++)
		{
			for ( int j = 0; j < squareNumber ; j++)
			{
				int tempa =  ( (i * squareSize) + (squareSize%2) );
				int tempb =  ( (j * squareSize) + (squareSize%2) );
				Square s = new Square( tempa ,  tempb );
				squares[i][j] =  s;
				
			}
		}
		
		add(new Board(800), BorderLayout.CENTER);	
	}
	public void initialCheck(Color inputColour)
	{
		if (movesRemaining > 0)
		{
			if (inputColour != squares[0][0].getColour() )
			{
			checkSquares(0,0, squares[0][0].getColour(), inputColour);
			squares[0][0].setColour(inputColour);
			movesRemaining--;
			}
		}
		
		
	}

	public void checkSquares(int one, int two, Color originalColour, Color newColour)
	{
		if(0 <= one && 0 <= two && one < squareNumber && two < squareNumber) 
		{
			if( squares[one][two].getColour() == originalColour)
			{
				squares[one][two].setColour(newColour);
				checkSquares( one + 1, two, originalColour, newColour);
				checkSquares( one - 1, two, originalColour, newColour);
				checkSquares( one, two + 1, originalColour, newColour);
				checkSquares( one, two - 1, originalColour, newColour);
			}
		}
	}	


	class Square 
	{
		Color colour = null;
		int x = 0;
		int y = 0;
		
		public Square (int xcord, int ycord)
		{
			x = xcord;
			y = ycord;
			this.setColour(  colours[  (int)(Math.random() * 6)  ]);
		}
		public void paint(Graphics g1)
		{
			Graphics2D r = (Graphics2D) g1;
			r.setPaint(colour);
			r.fillRect( x , y , squareSize, squareSize);
			
		}
		public void setColour( Color newColour )
		{
			colour = newColour;
		}
		public Color getColour( )
		{
			return colour;
		}
	
	}
	
	
	class Board extends JPanel
	{
		int boardSize = 0;
		int middlex = 0;
		int middley = 0;
		
		public Board(int size)
		{
			boardSize = size;
		}
		public void paint(Graphics g)
		{
			Graphics2D r = (Graphics2D)g;
			for(int i = 0; i < squares.length; i++) {
				for(int j = 0; j < squares[0].length; j++) {
					squares[i][j].paint(r);
				}
			}
			
		}
	}
	public static Image imageFromFile(String filename)
	{
		ImageIcon i = new ImageIcon(filename);
		if (i.getIconHeight() == -1 ) throw new RuntimeException( "Can not load image! >:O " + filename );
		return i.getImage();
	}
	
}